package com.ship.contract.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ship.common.core.base.TableConfig;
import com.ship.common.business.service.impl.BaseServiceImpl;
import com.ship.contract.mapper.TableConfigMapper;
import com.ship.contract.service.TableConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author tangshixuan
 * @date 2023-8-31
 */
@Service
public class TableConfigServiceImpl extends BaseServiceImpl<TableConfigMapper, TableConfig> implements TableConfigService {
    @Autowired(required = false)
    private TableConfigMapper tableConfigMapper;

    @Override
    public List<TableConfig> listByModuleAndUser(String module, Long currentUserId) {
        QueryWrapper queryWrapper =new QueryWrapper();
        queryWrapper.eq("module",module);
        queryWrapper.eq("user_id",currentUserId);
        queryWrapper.orderByAsc("order_num");
        return tableConfigMapper.selectList(queryWrapper);
    }
    @Override
    public List<String> listStrByModuleAndUser(String module, Long currentUserId) {
        return tableConfigMapper.listStrByModuleAndUser(module,currentUserId);
    }
    @Override
    public void saveConfig(JSONObject jsonObject, String currentUserId) {
        String module = jsonObject.getString("module");
        List<TableConfig> configs = this.baseMapper.selectList(
                new LambdaQueryWrapper<TableConfig>()
                        .eq(TableConfig::getModule,module)
                        .eq(TableConfig::getUserId,currentUserId)
                        .eq(TableConfig::getFixed,0)
        );
        Map<String,List<TableConfig>> originMap = configs.stream().collect(Collectors.groupingBy(TableConfig::getColumnKey));

        List<String> addList = new LinkedList<>();

        JSONArray columns = jsonObject.getJSONArray("columns");
        for (int i = 0;i < columns.size();i++){
            JSONObject o = columns.getJSONObject(i);
            String key = o.getString("columnKey");
            addList.add(key);
            if(originMap.get(key) != null){
                if(o.getInteger("orderNum").intValue() == originMap.get(key).get(0).getOrderNum()){
                    continue;
                }
                TableConfig updateConfig = originMap.get(key).get(0);
                updateConfig.setOrderNum(o.getInteger("orderNum"));
                super.updateById(updateConfig);
                continue;
            }

            TableConfig tableConfig = new TableConfig();
            tableConfig.setModule(module);
            tableConfig.setUserId(Long.valueOf(currentUserId));
            tableConfig.setFixed(0);
            tableConfig.setColumnKey(key);
            tableConfig.setOrderNum(o.getInteger("orderNum"));
            super.baseMapper.insert(tableConfig);
        }
        //移除
        List<String> removeList = new LinkedList<>();
        for (String s : originMap.keySet()) {
            if(addList.contains(s)){
                continue;
            }
            removeList.add(s);
        }

        if(removeList.size() > 0){
            this.baseMapper.delete(
                    new LambdaQueryWrapper<TableConfig>()
                            .in(TableConfig::getColumnKey,removeList)
            );
        }
    }
}
